﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IceStart.Core;
using IceStart.Utils;

namespace IceStart.View
{
    /// <summary>
    /// BaseMagnet.xaml 的交互逻辑
    /// </summary>
    public partial class FileMagnet : IMagnet
    {
        public static FileMagnet? DraggingMagnet = null;
        public bool IsClicking = false;

        public double nowTop = 0;
        public double nowLeft = 0;

        public FileMagnet(FileMagnetTie magnetTie)
        {
            MagnetTie = magnetTie;
            InitializeComponent();
            Canvas.SetZIndex(this, 1);
            if (magnetTie.ButtonSize == AbstractMagnetTie.Size.Large)
            {
                SetLarge();
            }
            if (magnetTie.ButtonSize == AbstractMagnetTie.Size.Medium)
            {
                SetMedium();
            }
            if (magnetTie.ButtonSize == AbstractMagnetTie.Size.Small)
            {
                SetSmall();
            }
            ContextMenu menu = new ContextMenu();
            MenuItem setBig = new MenuItem();
            setBig.Header = "设置为大图标";
            setBig.Click += new RoutedEventHandler((s, e) =>
            {
                SetLarge();
            });
            menu.Items.Add(setBig);

            MenuItem setMedium = new MenuItem();
            setMedium.Header = "设置为中图标";
            setMedium.Click += new RoutedEventHandler((s, e) =>
            {
                SetMedium();
            });
            menu.Items.Add(setMedium);

            MenuItem setSmall = new MenuItem();
            setSmall.Header = "设置为小图标";
            setSmall.Click += new RoutedEventHandler((s, e) =>
            {
                SetSmall();
            });
            menu.Items.Add(setSmall);

            MenuItem delete = new MenuItem();
            delete.Header = "删除磁贴";
            delete.Click += (s, e) =>
            {
                ((Canvas)VisualTreeHelper.GetParent(this)).Children.Remove(this);
            };
            menu.Items.Add(delete);

            this.ContextMenu = menu;
        }

        #region 拖动行为

        private Point startMousePos = new Point();
        private Point nowMousePos = new Point();
        private Point originPos = new Point();
        private bool isClicking = false;

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            MagnetBody.Background = (SolidColorBrush)Application.Current.Resources["MagnetColor_Selected"];
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            MagnetBody.Background = (SolidColorBrush)Application.Current.Resources["MagnetColor"];
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            nowMousePos = e.GetPosition(null);
            startMousePos = e.GetPosition(null);
            originPos.X = nowLeft;
            originPos.Y = nowTop;
            isClicking = true;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.LeftButton == MouseButtonState.Pressed && (DraggingMagnet == this || DraggingMagnet == null))
            {
                double dx = e.GetPosition(null).X - nowMousePos.X;
                double dy = e.GetPosition(null).Y - nowMousePos.Y;
                this.nowLeft += dx;
                this.nowTop += dy;
                if (nowTop < 0) nowTop = 0;
                if (nowLeft < 0) nowLeft = 0;
                Canvas.SetLeft(this, nowLeft);
                Canvas.SetTop(this, nowTop);
                nowMousePos = e.GetPosition(null);
                if (Math.Pow(nowLeft - startMousePos.X, 2) + Math.Pow(nowTop - startMousePos.Y, 2) > 100)
                {
                    Panel.SetZIndex(this, 100);
                    DraggingMagnet = this;
                    isClicking = false;
                }
            }
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            if (DraggingMagnet != null)
            {
                DraggingMagnet = null;
                Coordinates.RegisterPosition(this);
            }
            Canvas.SetZIndex(this, 1);
            if (isClicking)
            {
                Win32API.StartProgram(this.MagnetTie, Application.Current.MainWindow);
            }
        }

        #endregion 拖动行为

        #region 内联数据

        public FileMagnetTie MagnetTie;

        #endregion 内联数据

        public void SetPosition(double top, double left)
        {
            this.nowTop = top;
            nowLeft = left;
            if (nowTop < 0) nowTop = 0;
            if (nowLeft < 0) nowLeft = 0;
            Canvas.SetLeft(this, left);
            Canvas.SetTop(this, top);
        }

        public void SetLarge()
        {
            Height = Width = 240;
            FileIcon.Width = FileIcon.Height = 120;
            FileIcon.Margin = new Thickness(0, 47.5, 0, 0);
            FileName.Margin = new Thickness(10, 30, 10, 0);
            FileName.Visibility = Visibility.Visible;
            FileName.MaxHeight = 240 - 10 - 120 - 30;
            MagnetTie.ButtonSize = AbstractMagnetTie.Size.Large;
        }

        public void SetMedium()
        {
            Height = Width = 120;
            FileIcon.Width = FileIcon.Height = 60;
            FileIcon.Margin = new Thickness(0, 17.5, 0, 0);
            FileName.Margin = new Thickness(5, 15, 5, 0);
            FileName.Visibility = Visibility.Visible;
            FileName.MaxHeight = 120 - 10 - 60 - 15 - 17.5;
            MagnetTie.ButtonSize = AbstractMagnetTie.Size.Medium;
        }

        public void SetSmall()
        {
            Height = Width = 60;
            FileIcon.Width = FileIcon.Height = 40;
            FileIcon.Margin = new Thickness(7.5, 7.5, 7.5, 7.5);
            FileName.Visibility = Visibility.Hidden;
            MagnetTie.ButtonSize = AbstractMagnetTie.Size.Small;
        }
    }
}